import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:my_app/modules/navigator/navigator_util.dart';

///登录输入框，自定义widget
class JDTextField extends StatefulWidget {
  final String hintText;
  final ValueChanged<String>? editingCompleteCallBack;
  final ValueChanged<FocusNode>? focusNodeinitCallBack;

  final ValueChanged<bool>? focusChanged;
  final bool autoFocus;
  final TextInputType keyboardType;
  final int maxLines;
  final int maxLength;
  final Color? cursorColor;
  final TextStyle? textStyle;
  final TextStyle? hintStyle;
  final EdgeInsetsGeometry? contentPadding;
  final TextEditingController textEditController;
  final BorderSide? borderSide;

  final BorderRadius? borderRadius;

  final Color? fillColor;

  const JDTextField(this.hintText,
      {Key? key,
      required this.textEditController,
      this.focusChanged,
      this.autoFocus = false,
      this.maxLines = 1,
      this.maxLength = 1000,
      this.editingCompleteCallBack,
      this.focusNodeinitCallBack,
      this.textStyle,
      this.cursorColor,
      this.hintStyle,
      this.contentPadding,
      this.fillColor,
      this.borderSide,
      this.borderRadius,
      this.keyboardType = TextInputType.text})
      : super(key: key);

  @override
  State<JDTextField> createState() => _JDTextFieldState();
}

class _JDTextFieldState extends State<JDTextField> {
  final _focusNode = FocusNode();

  @override
  void initState() {
    super.initState();
    //是否获取光标的监听
    _focusNode.addListener(() {
      // tdLog("Has focus: ${_focusNode.hasFocus}");
      if (widget.focusChanged != null) {
        widget.focusChanged!(_focusNode.hasFocus);
      }
    });

    widget.focusNodeinitCallBack?.call(_focusNode);
  }

  @override
  void dispose() {
    _focusNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Row(
          children: [_input()],
        ),
      ],
    );
  }

  _input() {
    return Expanded(
        child: TextField(
            controller: widget.textEditController,
            maxLength: widget.maxLength,
            focusNode: _focusNode,
            keyboardType: widget.keyboardType,
            textInputAction: TextInputAction.done,
            autofocus: widget.autoFocus,
            maxLines: widget.maxLines,
            onEditingComplete: () {
              NavigatorUtil.unFocus();
              widget.editingCompleteCallBack
                  ?.call(widget.textEditController.text);
            },
            //光标颜色
            cursorColor: widget.cursorColor ?? Colors.grey,
            // cursorHeight: 5,
            style: widget.textStyle ??
                const TextStyle(fontSize: 16, fontWeight: FontWeight.w300),
            inputFormatters: (widget.keyboardType == TextInputType.number ||
                    widget.keyboardType == TextInputType.phone)
                ? [FilteringTextInputFormatter.allow(RegExp('[0-9]'))]
                : null,
            //输入框的样式
            decoration: InputDecoration(
              fillColor: widget.fillColor,
              filled: widget.fillColor != null ? true : false,
              contentPadding: widget.contentPadding ??
                  const EdgeInsets.fromLTRB(10, 10, 10, 10),
              counterText: '',
              border: OutlineInputBorder(
                borderSide: widget.borderSide ?? BorderSide.none,
                borderRadius: widget.borderRadius ??
                    const BorderRadius.all(Radius.circular(0)),
              ),
              focusedBorder: OutlineInputBorder(
                borderSide: widget.borderSide ?? BorderSide.none,
                borderRadius: widget.borderRadius ??
                    const BorderRadius.all(Radius.circular(0)),
              ),
              enabledBorder: UnderlineInputBorder(
                borderSide: widget.borderSide ?? BorderSide.none,
                borderRadius: widget.borderRadius ??
                    const BorderRadius.all(Radius.circular(0)),
              ),
              hintText: widget.hintText,
              hintStyle: widget.hintStyle ?? const TextStyle(fontSize: 16),
              floatingLabelBehavior: FloatingLabelBehavior.never, // 禁止浮动行为
            )));
  }
}
